package org.hzero.oauth.utils;

import io.choerodon.core.oauth.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class JwtUtils  {
    @Autowired
    private  TokenStore tokenStore;

    public String tokenGenerator() {

        HashMap<String, String> requestParams = new HashMap<>();
        HashSet<String> scopes = new HashSet<>();
        HashSet<String> responseType = new HashSet<>();
        OAuth2Request oAuth2Request = new OAuth2Request(
                requestParams,
                "hzero-front-dev",
                null,
                true,
                scopes,
                scopes,
                "http://localhost:8002/workplace",
                responseType,
                null
        );
        CustomUserDetails userDetails = new CustomUserDetails("admin", "Admin@123!");
        userDetails.setUserId(2L);
        userDetails.setRealName("超级管理员");
        userDetails.setEmail("admin@hzero.com");
        userDetails.setTimeZone("GMT+8");
        userDetails.setLanguage("zh_CN");
        userDetails.setRoleId(1L);
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(1L);
        roleIds.add(2L);
        userDetails.setRoleIds(roleIds);
        userDetails.setRoleIds(roleIds);
        ArrayList<Long> tenantRoleIds = new ArrayList<>();
        tenantRoleIds.add(2L);
        userDetails.setTenantRoleIds(tenantRoleIds);
        userDetails.setRoleMergeFlag(false);
        HashMap<Long, String> longStringHashMap = new HashMap<>();
        longStringHashMap.put(1L, "GEN");
        longStringHashMap.put(2L, "GEN");
        userDetails.setRoleTypes(longStringHashMap);
        userDetails.setTenantId(0L);
        userDetails.setTenantNum("HZERO");
        ArrayList<Long> tenantlIds = new ArrayList<>();
        tenantlIds.add(0L);
        userDetails.setTenantIds(tenantlIds);
        userDetails.setOrganizationId(0L);
        userDetails.setAdmin(true);
        userDetails.setApiEncryptFlag(1);
        userDetails.setApiReplayFlag(0);
        userDetails.setMenuIdFlag(0);

        JwtGenerator generator = new JwtGenerator();
        String token = generator.generateJwtToken("admin", "超级管理员");
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null);

        OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, usernamePasswordAuthenticationToken);

        DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(token);
        tokenStore.storeAccessToken(oAuth2AccessToken, oAuth2Authentication);

        return token;
    }

}
